/*
 * Encog(tm) Examples v2.4
 * http://www.heatonresearch.com/encog/
 * http://code.google.com/p/encog-java/
 * 
 * Copyright 2008-2010 by Heaton Research Inc.
 * 
 * Released under the LGPL.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 * 
 * Encog and Heaton Research are Trademarks of Heaton Research, Inc.
 * For information on Heaton Research trademarks, visit:
 * 
 * http://www.heatonresearch.com/copyright.html
 */

package mtamarket;

import java.io.File;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;

import org.encog.ConsoleStatusReportable;
import org.encog.engine.network.activation.ActivationTANH;
import org.encog.ml.data.MLDataSet;
import org.encog.ml.data.basic.BasicMLData;
import org.encog.ml.data.basic.BasicMLDataPair;
import org.encog.ml.data.basic.BasicMLDataSet;
import org.encog.neural.data.NeuralDataSet;
import org.encog.neural.pattern.FeedForwardPattern;

import org.encog.persist.EncogDirectoryPersistence;
import org.encog.util.simple.EncogUtility;

public class MarketPrune {

	public static void incremental(File dataDir, Config config) {

		double[][] data = config.transformedData;
		
		FeedForwardPattern pattern = new FeedForwardPattern();
		pattern.setInputNeurons(data[0].length);
		pattern.setOutputNeurons(config.numberOfForecasts);
		pattern.setActivationFunction(new ActivationTANH());

		MLDataSet training = getMLDataSet(data);
		
		PruneIncremental prune = new PruneIncremental(training, pattern, 100, 1, 10,
				new ConsoleStatusReportable());

		prune.addHiddenLayer(5, 50);
		prune.addHiddenLayer(0, 50);

		prune.process();
		
		System.out.println("\n\nBest Network:");
		System.out.println("Structure: " + PruneIncremental.networkToString(prune.getBestNetwork()));
		System.out.println("Percent Error: " + prune.bestError * 100 + "%");
		
		File networkFile = new File(dataDir, Config.NETWORK_FILE);
		EncogDirectoryPersistence.saveObject(networkFile, prune.getBestNetwork());
		
		BobsUtility.saveNetwork(prune.getBestNetwork());

	}
	
	public static BasicMLDataSet getMLDataSet(double[][] data){
		BasicMLDataSet toReturn = new BasicMLDataSet();
		
		BasicMLData input;
		BasicMLData ideal;
		
		double[] idealArray = new double[Config.numberOfForecasts];
		
		double[] inputArray = new double[data[0].length];
		
		int sizeOfData = data.length;
		
		for(int i = 0; i < sizeOfData - 2 * Config.numberOfForecasts; i++){
			for(int j = 1; j <= Config.numberOfForecasts; j++){
				idealArray[j - 1] = data[i + j][0];
			}
			
			ideal = new BasicMLData(idealArray);
			
			inputArray = data[i];
			
			input = new BasicMLData(inputArray);
			
			toReturn.add(input, ideal);
			
		}
	
				
		return toReturn;
	}
}
